#pragma once
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

#include "../log.h"

class Util{
public:
    static size_t Split(std::string &src, const std::string &sep, std::vector<std::string> *out){

        size_t offset = 0;
        size_t sz = src.size();
        while(offset < sz){
            size_t pos = src.find(sep, offset);
            if(pos == std::string::npos){
                out->push_back(src.substr(offset));
                return out->size();
            }
            if(pos == offset) {
                offset += sep.size();
                continue;
            }
            out->push_back(src.substr(offset, pos - offset));
            offset = pos + sep.size();
        }
        return out->size();
    }
    static bool ReadFile(const std::string &path, std::string *buf){
        std::ifstream ifs(path, std::ios::binary);
        if(!ifs.is_open()){
            ERROR_LOG("Open %s File Failed", path.c_str());
            // ifs.close();
            return false;
        }

        size_t fsize = 0;
        ifs.seekg(0, ifs.end);
        fsize = ifs.tellg();
        ifs.seekg(0);
        buf->resize(fsize);
        ifs.read(&(*buf)[0], fsize);
        if(ifs.good() == false){
            ERROR_LOG("Read %s File Failed", path.c_str());
            // ifs.close();
            return false;
        }
        // ifs.close();
        return true;
    }
    static bool WriteFile(const std::string path, const std::string &buf){
        std::ofstream ofs(path, std::ios::binary | std::ios::trunc);
        if(!ofs.is_open()){
            ERROR_LOG("Open File %s For Write Failed", path.c_str());
            return false;
        }
        ofs.write(buf.c_str(), buf.size());
        if(!ofs.good()){
            ERROR_LOG("Write File %s Failed", path.c_str());
            return false;
        }
        return true;
    }
};

class Http{
private:


};